//
//  DetailViewController.m
//  test2
//
//  Created by yuzhou on 13-12-9.
//  Copyright (c) 2013年 yuzhou. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

#pragma mark - Managing the detail item

//- (void)setDetailItem:(id)newDetailItem
//{
//    if (_detailItem != newDetailItem) {
//        _detailItem = newDetailItem;
//        
//        // Update the view.
//        [self configureView];
//    }
//}
- (void)insertNewObject
{
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject ;
    if ([self.PushIdentifier isEqualToString:@"add"]) {
        newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    }else if ([self.PushIdentifier isEqualToString:@"showDetail"]){
        newManagedObject = self.detailItem;
    }
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:self.detailDescriptionLabel.text];

    [newManagedObject setValue:date forKey:@"timeStamp"];
    [newManagedObject setValue:self.nameField.text forKey:@"name"];
    [newManagedObject setValue:[NSNumber numberWithFloat:self.AgeSlider.value]  forKey:@"slider"];
    [newManagedObject setValue:[NSNumber numberWithInteger:self.sexSegmented.selectedSegmentIndex] forKey:@"segmented"];
    [newManagedObject setValue:[NSNumber numberWithBool:self.MarrySwitch.isOn] forKey:@"switch"];
    
    NSError *error = nil;
    if (![context save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}
- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        
        self.searchButton.hidden = YES;
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *strDate = [dateFormatter stringFromDate:[self.detailItem valueForKey:@"timeStamp"]];

        self.detailDescriptionLabel.text =strDate;
        self.nameField.text = [[self.detailItem valueForKey:@"name"] description];
        self.AgeSlider.value =[[[self.detailItem valueForKey:@"slider"] description] floatValue];
        self.AgeValue.text = [NSString stringWithFormat:@"%.0f",self.AgeSlider.value];

        self.sexSegmented.selectedSegmentIndex =[[self.detailItem valueForKey:@"segmented"] integerValue];
        BOOL isOn = [[self.detailItem valueForKey:@"switch"] intValue];
        [self.MarrySwitch setOn:isOn animated:YES];
    }else{
        self.searchButton.hidden = NO;
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
        self.detailDescriptionLabel.text = strDate;
        
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)AgeAction:(id)sender {
    self.AgeValue.text=[NSString stringWithFormat:@"%.0f",self.AgeSlider.value];
}
- (IBAction)saveButton:(id)sender {
    [self insertNewObject];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"保存成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (IBAction)searchButton:(id)sender {
    NSError * error;
    NSArray *array = [self.managedObjectContext executeFetchRequest:self.fetchedResultsController.fetchRequest error:&error];
    for (NSArray *obj in array) {
        if ([[obj valueForKey:@"name"] isEqualToString:self.nameField.text]) {
            self.detailItem = obj;
            self.PushIdentifier=@"showDetail";
            [self configureView];
        }
    }
    
}
@end
